import webpack from 'webpack';
import chalk from 'chalk';
import getWebpackConfig from '../getWebpackConfig';
import logger from '../logger';

export default () => {
  const webpackConfig = getWebpackConfig();
  return webpack(webpackConfig, (err, stats) => {
    if (err) {
      logger.error(chalk.red(err));
      return;
    }
    const logOptions = {
      all: false,
      version: true,
      timings: true,
      modules: false,
      modulesSpace: 0,
      assets: false,
      assetsSpace: 0,
      errors: true,
      errorsCount: true,
      warnings: true,
      warningsCount: true,
      logging: 'warn',
    };
    if (stats && stats.hasErrors()) {
      logger.error(chalk.red(stats.toString(logOptions)));
    }
    if (stats && stats.hasWarnings()) {
      // 在这里处理错误
      logger.warn(chalk.yellow(stats.toString(logOptions)));
    }
    logger.success(chalk.green('Complete !'));
    // 处理完成
  });
};
